package org.davidliebman.android.awesomewifi;

import java.util.ArrayList;


import android.content.Context;
import android.graphics.Color;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiInfo;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/* special adapter for displaying list from ArrayList */
public class RecordAdapter extends ArrayAdapter {
	
	public static final String TAG = new String("RecordAdapter");
	ArrayList<WifiConfiguration> mList = new ArrayList<WifiConfiguration>();
	ArrayList<ScanResult> mScans = new ArrayList<ScanResult>();
	ArrayList<WifiListNode> mCombined = new ArrayList<WifiListNode> ();
	Context mContext;
	private int mPosition;
	private int mMode;
	public WifiInfo info;
	
	public static final int MODE_CONFIGURATION = 1;
	public static final int MODE_SCANRESULT = 2;
	public static final int MODE_COMBINED = 3;
	
	public RecordAdapter(Context context, int resourceID, ArrayList<WifiListNode> list, int mode, WifiInfo mI ) {
		super (context, resourceID, list);
		this.mCombined = list;
		this.info = mI;
		mContext = context;
		mMode = MODE_COMBINED;
	}
	
	@Override
    public View getView(int position, View convertView, ViewGroup parent) {
		
		LayoutInflater inflater = (LayoutInflater)mContext.getSystemService (Context.LAYOUT_INFLATER_SERVICE);
		convertView = inflater.inflate(R.layout.data_wifi_row, null);
		boolean mOKIcon = false;
		
		mPosition = position;


		WifiListNode mNode = mCombined.get(position);
		ScanResult mScan = null;
		WifiConfiguration mRec = null;
		
		if (mNode.getContents() == WifiListNode.CONTAINS_BOTH || 
				( mNode.getContents() == WifiListNode.CONTAINS_SCANS &&
				mNode.getEncryption() == Wifi.ENCRYPT_NONE )) {
			mOKIcon = true;
		}
		
		if (mNode.getListInfo() == WifiListNode.CONTAINS_SCANS) {
			mScan = mNode.getScan();
			this.showScan(convertView, mScan, mNode.getDescription(), mNode.getEncryption(), mOKIcon);
		}
		else if (mNode.getListInfo() == WifiListNode.CONTAINS_CONFIG)  {
			mRec = mNode.getConfig();
			this.showConfig(convertView, mRec, "Not In Range...", mOKIcon);
		}
		
		else if (mNode.getListInfo() == WifiListNode.CONTAINS_BOTH) {
			mRec = mNode.getConfig();
			this.showConfig(convertView, mRec, "In Range...", mOKIcon);	
		}
		
		
		
		return convertView;
	}
	
	public void showConfig(View convertView, WifiConfiguration mRec, String mConnect, boolean mOKIcon) {

		TextView mSSID = (TextView) convertView.findViewById(R.id.text_ssid);
		TextView mStrength = (TextView) convertView.findViewById(R.id.text_strength);
		TextView mConnected = (TextView) convertView.findViewById(R.id.text_connected);
		ImageView mImage = (ImageView) convertView.findViewById(R.id.icon);
		
		mSSID.setText("Name: " +mRec.SSID );
		mStrength.setText("Configured WiFi: "+showStatus( mRec.status));
		mConnected.setText(mConnect);
	
		if (info.getNetworkId() == mRec.networkId ) {
			hilightView(convertView);
			mConnected.setText("Connected");
		}

		else if (mOKIcon) {
			//
			mImage.setImageResource(R.drawable.ic_chooseme);
		}
		else mImage.setImageResource(R.drawable.ic_list);
	}
	
	public void showScan(View convertView, ScanResult mScan, String mConnect, int mEncryption , boolean mOKIcon) {

		TextView mSSID = (TextView) convertView.findViewById(R.id.text_ssid);
		TextView mStrength = (TextView) convertView.findViewById(R.id.text_strength);
		TextView mConnected = (TextView) convertView.findViewById(R.id.text_connected);
		ImageView mImage = (ImageView) convertView.findViewById(R.id.icon);
				
		mSSID.setText("Name: " + mScan.SSID );
		mStrength.setText("Strength: "+ mScan.level + " dBm");
		mConnected.setText(mConnect);
		
		
		if ((info.getSSID() != null) && mScan.SSID.contentEquals(info.getSSID().toString())  ) {
			hilightView(convertView);
			mConnected.setText("Connected");
		}
		else {
			//mConnected.setText(mScan.capabilities);
		}

		if (mOKIcon) {
			mImage.setImageResource(R.drawable.ic_chooseme);
		}
		else if (mEncryption != Wifi.ENCRYPT_DETECTNONE ) {
			mImage.setImageResource(R.drawable.ic_yellow);
		}
		else if (mEncryption  == Wifi.ENCRYPT_DETECTNONE || 
				mEncryption == Wifi.ENCRYPT_NONE ) {
			mImage.setImageResource(R.drawable.ic_chooseme);
		}
		
	}
	
	public void hilightView(View convertView) {
		TextView mSSID = (TextView) convertView.findViewById(R.id.text_ssid);
		TextView mStrength = (TextView) convertView.findViewById(R.id.text_strength);
		TextView mConnected = (TextView) convertView.findViewById(R.id.text_connected);
		ImageView mImage = (ImageView) convertView.findViewById(R.id.icon);
		
		mImage.setImageResource(R.drawable.ic_launcher);
		convertView.findViewById(R.id.item_holder).setBackgroundColor(Color.GRAY);
		mSSID.setTextColor(Color.BLACK);
		mStrength.setTextColor(Color.BLACK);
		mConnected.setTextColor(Color.BLACK);
	}
	
	public String showStatus(int mIn) {
		String mStat = new String();
		switch(mIn) {
		case 0: mStat = new String("CURRENT"); break;
		case 1: mStat = new String("DISABLED"); break;
		case 2: mStat = new String("ENABLED"); break;
		}
		return mStat;
	}
};
